package org.zpmis.common.result;

import org.zpmis.common.enums.LangEnum;
import org.zpmis.common.utils.StringUtil;
import org.zpmis.common.utils.language.LanguageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * 统一返回状态码
 *
 * @author LENOVO
 */
public enum ResultCode {

    /* 成功状态码 */
    SUCCESS(200, "成功"),
    ERROR(999,"系统繁忙，请稍后再试！"),
    /* 参数错误：10001-19999 */
    PARAM_ERROR(10001, "参数错误"),

    /* 用户错误 */
    USER_NOT_LOGIN(2001, "用户未登录"),
    USER_ACCOUNT_EXPIRED(2002, "账号已过期"),
    USER_CREDENTIALS_ERROR(2003, "密码错误"),
    USER_CREDENTIALS_EXPIRED(2004, "密码过期"),
    USER_ACCOUNT_DISABLE(2005, "账号不可用"),
    USER_ACCOUNT_LOCKED(2006, "账号被锁定"),
    USER_ACCOUNT_NOT_EXIST(2007, "账号不存在"),
    USER_ACCOUNT_ALREADY_EXIST(2008, "账号已存在"),
    USER_ACCOUNT_USE_BY_OTHERS(2009, "账号下线"),
    TOKEN_ERROR(2010, "TOKEN不可用"),
    USER_NOT_EXIST(3000,"用户未找到"),
    NO_PERMISSION(3001, "没有权限"),
    USER_LOGIN_ERROR(3002, "登录失败"),
    USER_NAME_ALREADY_EXIST(3003, "用户名已存在"),

    /**
     * 订单错误
     */
    ORDER_NOT_FOUND(4001,"订单不存在"),
    DATA_NOT_FOUND(4004,"数据未找到"),
    ORDER_STATUS_ERROR(4005,"订单状态错误"),
    ORDER_COLLECTION_ERROR(4006,"只能对待发货的订单收款"),
    ORDER_CONFIRM_ERROR(4007,"当前订单状态不允许确认收货！"),
    ORDER_APPLY_CHECK_ERROR(4008, "当前用户未签订入网合同，不允许申请订单！"),
    ORDER_DETAIL_NOT_EDIT(4009,"已收款明细不允许修改"),
    ORDER_DETAIL_NOT_DELETED(4010,"已收款明细不允许删除"),
    ORDER_DETAIL_COLLECTION(4011,"当前明细已收款,请勿重复操作!"),
    ORDER_NOT_DELETED(4012,"已收款订单不允许废弃"),
    ORDER_DISCARD_STATUS(4013,"订单已经废弃"),
    /**
     * 业务提示
     */
    DATA_ALREADY_EXISTS(5000,"数据已存在"),
    DATA_ALREADY_CHILD_EXISTS(5001,"数据有子节点，请先删除子节点"),
    INTENTION_RETURNVISIT_EXISTS(5002,"该意向客户已经回访！"),
    DATA_ALREADY_RELATION_EXISTS(5003,"该记录有关联数据，请先删除关联数据。"),
    SYS_ACCOUNT_OCCUPIED(5004, "账号已经分配给其他员工，不可重复分配！"),
    SYS_NUMBER_ERROR(5005,"服务繁忙，请稍后重试！"),
    INVOICE_LOGISTICSNUM_ERROR(5006,"物流单号不能为空！"),
    INVOICE_ATTACHMENT_ERROR(5007,"附件信息不能为空！"),
    INVOICE_APPLY_ERROR(5008,"当前订单已经申请过发票，请勿重复申请！"),
    INVOICE_ATTACHMENT_EMPTY(5009,"当前发票没有附件信息！"),
    INVOICE_NOT_AUDIT(5010,"当前发票还未审核成功！"),
    EMAIL_SEND_ERROR(5011,"邮件发送失败！"),
    INVOICE_GET_ATTACHMENT_ERROR(5012,"附件信息获取失败！"),
    COMPANY_DATA_ALREADY_EXISTS(5013,"企业信息已存在"),
    WX_TEMPLATE_SEND_ERROR(5014,"公众号模板消息发送失败"),
    CARE_PLAN_ALREADY_FINISH(5015,"客户关怀计划已完结，不可删除！"),
    CARE_PLAN_ALREADY_EXECUTE(5016,"客户关怀计划已执行，不可再次执行！"),

    COMPANY_CODE_NOT_EMPTY(6001,"企业Code不能为空！"),
    ;

    private Integer code;

    private String msg;

    ResultCode(Integer code, String message) {
        this.code = code;
        this.msg = StringUtil.isEmpty(LanguageUtils.getMessage(this.name())) ? message :  LanguageUtils.getMessage(this.name());

    }

    public static ResultCode transferParams(ResultCode resultCode, Object[] params) {
        resultCode.msg = LanguageUtils.getMessage(resultCode.name(),params, LangEnum.CN.code());
        return resultCode;
    }

    public Integer code() {
        return this.code;
    }

    public void setMessage(String message) {
        this.msg = StringUtil.isEmpty(LanguageUtils.getMessage(this.name()))  ? message : LanguageUtils.getMessage(this.name());
    }

    public void setMessageStr(String message) {
        this.msg = message;
    }

    public String message() {
        return StringUtil.isEmpty(LanguageUtils.getMessage(this.name())) ? msg : LanguageUtils.getMessage(this.name());
    }

    public String defaultMessage() {
        return this.msg;
    }

    public static String getMessage(String name) {
        for (ResultCode item : ResultCode.values()) {
            if (item.name().equals(name)) {
                return item.msg;
            }
        }
        return name;
    }

    public static Integer getCode(String name) {
        for (ResultCode item : ResultCode.values()) {
            if (item.name().equals(name)) {
                return item.code;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return this.name();
    }

    //校验重复的code值
    public static void main(String[] args) {
        ResultCode[] apiResultCodes = ResultCode.values();
        List<Integer> codeList = new ArrayList<>();
        for (ResultCode apiResultCode : apiResultCodes) {
            if (codeList.contains(apiResultCode.code)) {
                System.out.println(apiResultCode.code);
            } else {
                codeList.add(apiResultCode.code());
            }
        }
    }
}
